package com.fit.pages;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.BorrowBean;
import com.fit.beans.DamageEncoder;
import com.fit.beans.DamageSelectModel;

import com.fit.beans.RecordDamageBean;
import com.fit.interfaces.Borrow;
import com.fit.interfaces.Damage;
import com.fit.interfaces.RecordDamage;
import com.fit.interfaces.User;
import com.fit.service.BorrowUniqueID;
import com.fit.service.Borrows;
import com.fit.service.Damages;
import com.fit.service.RecordDamages;

public class RecordsOfDamagedCars {

	
	@SessionState
	private User user;
	
	
	private boolean userExists;
	
	
	public User getUser(){
		return user;
	}
	
	Object onActivate(){
		if(!userExists) return Login.class;
		return null;
	}
	//@Inject
	@Persist
	private Damage selectedDamage;
	

	@Persist
	@Property
	private String comment;

	@Persist
	private String passedId;
	
	@Inject
	private Borrows borrows;
	
	@Persist
	@Property
	private RecordDamage recordDamage;
	
	@InjectPage
	private ViewRecordDamage viewRecordDamagePage;
	
	
//	@Property
//	private Borrow borrow;
	
	
	@Inject
	@Property
	private RecordDamages recordDamages;
	
	@Inject
	@Property
	private Damages damages;

	public Damage getSelectedDamage() {
		return selectedDamage;
	}
	@Validate("required")
	public void setSelectedDamage(Damage selectedDamage) {
		this.selectedDamage = selectedDamage;
	}

	public String getPassedId() {
		return passedId;
	}

	public void setPassedId(String passedId) {
		this.passedId = passedId;
	}
	

	public SelectModel getDamageModel(){
		return new DamageSelectModel(damages.retrive());
	}
	
	public ValueEncoder getDamageEncoder(){
		return new DamageEncoder(damages);
	}
	
	@SetupRender
	public void createObject(){
		RecordDamage record = new RecordDamageBean();
		record.setDamage(selectedDamage);
	}
	
	@OnEvent
	Object onSubmit(){
		Borrow borrow = null;
		borrow = borrows.retriveSingleBorrow(getPassedId());
		RecordDamage damageBean = new RecordDamageBean();
		Damage dd = selectedDamage;
		damageBean.executeDamages(dd,comment, borrow);
		recordDamages.save(damageBean);
		viewRecordDamagePage.setPassedId(getPassedId());
		viewRecordDamagePage.setPassedDamage(dd.getName());
		passedId = "";
		comment = "";
		selectedDamage = null;
		return viewRecordDamagePage;
	}
	
	
	
}
